import requests

BASE_URL = "http://localhost:8080/api/auth/login"

def login(username, password):
    url = f"{BASE_URL}"
    data = {
        "username": username,
        "password": password
    }
    response = requests.post(url, params=data)

    # Check if the login was successful
    if response.status_code == 200:
        response_data = response.json()
        if response_data.get("success"):
            # Store the token in a file
            token = response_data["data"]["token"]
            with open("token.txt", "w") as token_file:
                token_file.write(token)
            return token
        else:
            print("Login failed:", response_data.get("message"))
    else:
        print(f"Error {response.status_code}: {response.text}")
    return None

if __name__ == "__main__":
    # Test the login function
    print("Logging in user...")
    login_response = login("ssss", "Test12www35")
    if login_response:
        print("Login successful, token saved.")
    else:
        print("Login failed.")